﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace AutomationTools
{
    public static class StringMethodExtension
    {
        /// <summary>
        /// Converte o texto em um HtmlAgilityPack.HtmlDocument
        /// </summary>
        /// <param name="that">Texto a ser convertido em HTML</param>
        /// <returns>Documento HTML do AgilityPack</returns>
        public static HtmlAgilityPack.HtmlDocument ParseHtml(this string that)
        {
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(that);
            return doc;
        }

        public static IDynamicMetaObjectProvider ToExpando(this object that)
        {
            ExpandoObject expando = new ExpandoObject();
            Type tipo = that.GetType();
            foreach (var item in tipo.GetProperties())
            {
                ((IDictionary<String, Object>)expando).Add(item.Name, item.GetValue(that, null));
            }
            return expando;
        }

        public static IEnumerable<TSource> DuplicatesBy<TSource, TKey>
    (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                // Yield it if the key hasn't actually been added - i.e. it
                // was already in the set
                if (!seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }

    }
}
